<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/27
 * Time: 下午2:20
 */

class RealTimeController extends AdminBaseController {

    public function fiveMinutes(){
        $this->setTitle('5分钟实时数据');
        $filter = new PubFilter();
        $grid   = new PubGrid();
        $chart  = new PubChart();

        $kpiOptions = array(
            'ALL'       => PubFunc::t('所有指标'),
            'PayMoney'  => PubFunc::t('充值金额'),
            'PayUser'   => PubFunc::t('充值人数'),
        	'PayCount'   => PubFunc::t('充值人次'),
        	'FirstPaymoney'  => PubFunc::t('首次充值金额'),
        	'FirstPayUser'   => PubFunc::t('首次充值人数'),
        	'FirstPayCount'   => PubFunc::t('首次充值人次'),
            'LoginUser' => PubFunc::t('登录数'),
            'RoleUser'  => PubFunc::t('创建角色数'),
        	'RegUser'  => PubFunc::t('新注册账号数'),
            //'Online'    => PubFunc::t('在线')
        );
        
        $lod_special_id = $this->R('special_id');
        $lodos = $this->R('os');
        $filter->addSpecialFilter(false,2);
        $filter->addOsFilter($lod_special_id,true,2)
        ->addServerFilter($lod_special_id,$lodos,true,false,2);

        //$filter->addPlatformFilter();
        //$filter->addServerFilter();
        $filter->addFilter('date', 'input', PubFunc::t('日期'), array('value' => date('Y-m-d',time()), 'date' => 'day'))
            //->addFilter('separate', 'checkbox', ' ', array('options' => array('true' => '分服展示'), 'value' => ''))
            ->addFilter('br', 'br')
            ->addFilter('kpi', 'link', PubFunc::t('KPI指标'), array('options' => $kpiOptions, 'value' => 'ALL', 'fixed'=>true));

        $params = $filter->get();
        $params = $this->indexCommon($params);
        
        if(!array_key_exists($params['kpi'],$kpiOptions)){
            $params['kpi'] = "ALL";
        }

        $data = RealTimeService::fiveMinuteData($params);       

        if($params['kpi'] == 'Online' && $params['separate'][0] == true){
        	$flag = true;
        }
        
        $tableData = RealTimeService::fiveMinutesTableData($data,$params);

        $chartData = RealTimeService::fiveMinutesChartData($data, $flag);

        $grid->addGrid(RealTimeService::fiveMinutesHeader($params['date']),$tableData, array(
            'name'  => $kpiOptions[$params['kpi']],
        	'length'=>50,
            'fix' => array(
                'row' => true,
            	'col' => 3
            )
        ));

        $config = array(
            'normal' => array(
                'toolbox'   => array(
                    'show'  => false
                ),
                'grid'  => array(
                    'x'  => 50,
                    'y'  => 30,
                    'x2' => 50,
                    'y2' => 30
                )
            ),
            'series' => array(
                PubFunc::t('充值金额') => array(
                    'symbolSize' => 0,
                ),
                PubFunc::t('充值人数') => array(
                    'symbolSize' => 0,
                ),
                PubFunc::t('登录数') => array(
                    'symbolSize' => 0,
                ),
                PubFunc::t('创建角色数') => array(
                    'symbolSize' => 0,
                ),
                PubFunc::t('去重创角用户数') => array(
                    'symbolSize' => 0,
                ),
                PubFunc::t('在线') => array(
                    'symbolSize' => 0,
                    'markLine' => array(
                        'data' => array(
                            array('type' => 'average','name' => PubFunc::t('平均值'))
                        )
                    )
                ),

            )
        );
        
        if($params['kpi'] === 'ALL'){
//             $chart->addChart('Online', $chartData['Online'], array(
//                 'type'   => 'line',
//                 'config' => $config
//             ));
            $chart->addChart('LoginUser', $chartData['LoginUser'], array(
                'type'   => 'line',
                'config' => $config
            ));
            $chart->addChart('PayUser', $chartData['PayMoney'], array(
                'type'   => 'line',
                'config' => $config
            ));
            $chart->addChart('RoleUser', $chartData['RoleUser'], array(
            		'type'   => 'line',
            		'config' => $config
            ));
//             $chart->addChart('RoleUser', $chartData['RoleUser'] + $chartData['AccountUser'], array(
//                 'type'   => 'line',
//                 'config' => $config
//             ));

            $chart->setShowType('float');
        }else{
        	
        	if($flag == true){
        		
        		$sids = array_keys($chartData['Online']);
        		foreach($sids as $sid){
        			$con[$sid] = array('symbolSize' => 0);
        		}
        		$config['series'] = $con;
        	}

        	$chart->addChart('chart1', $chartData[$params['kpi']], array(
        			'type'   => 'line',
       				'config' => $config,
        			'name'	 => $kpiOptions[$params['kpi']]
       		));
            
        }

        $this->out['filter'] = $filter->show();
        $this->out['grid']   = $grid->show();
        //$this->out['chart']  = $chart->show();
        $this->out['is_ana'] = 2;
    }

    public function perHour(){
        $this->setTitle('小时数据');
        $filter = new PubFilter();
        $grid   = new PubGrid();
        $chart  = new PubChart();

        $kpiOptions = array(
            'ALL'       => PubFunc::t('所有指标'),
            'PayMoney'  => PubFunc::t('充值金额'),
            'PayUser'   => PubFunc::t('充值人数'),
            'LoginUser' => PubFunc::t('登录数'),
            'RoleUser'  => PubFunc::t('创建角色数'),
            'Online'    => PubFunc::t('在线')
        );

        $filter->addPlatformFilter()
            ->addServerFilter()
            ->addFilter('date', 'input', PubFunc::t('日期'), array('value' => date('Y-m-d',time()), 'date' => 'day'))
            ->addFilter('separate', 'checkbox', ' ', array('options' => array('true' => '分服展示'), 'value' => ''))
            ->addFilter('br', 'br')
            ->addFilter('kpi', 'link', PubFunc::t('KPI指标'), array('options' => $kpiOptions, 'value' => 'ALL', 'fixed'=>true));

        $params = $filter->get();

        if(!array_key_exists($params['kpi'],$kpiOptions)){
            $params['kpi'] = "ALL";
        }

        $data = RealTimeService::perHourData($params);

        $today = (date('Y-m-d') == $params['date']);

        $tableData = RealTimeService::perHourTableData($data);
        $chartData = RealTimeService::perHourChartData($data, $today);

        $grid->addGrid(RealTimeService::perHourHeader($params['date']),$tableData, array(
            'name'  => $kpiOptions[$params['kpi']],
            'fix' => array(
                'row' => true
            )
        ));

        $config = array(
            'normal' => array(
                'toolbox'   => array(
                    'show'  => false
                ),
                'grid'  => array(
                    'x'  => 50,
                    'y'  => 30,
                    'x2' => 50,
                    'y2' => 30
                )
            ),
            'series' => array(
                PubFunc::t('充值金额') => array(
                    'symbolSize' => 0,
                ),
                PubFunc::t('充值人数') => array(
                    'symbolSize' => 0,
                ),
                PubFunc::t('登录数') => array(
                    'symbolSize' => 0,
                ),
                PubFunc::t('创建角色数') => array(
                    'symbolSize' => 0,
                ),
                PubFunc::t('去重创角用户数') => array(
                    'symbolSize' => 0,
                ),
                PubFunc::t('在线') => array(
                    'symbolSize' => 0,
                    'markLine' => array(
                        'data' => array(
                            array('type' => 'average','name' => PubFunc::t('平均值'))
                        )
                    )
                ),

            )
        );

        if($params['kpi'] === 'ALL'){
            $chart->addChart('Online', $chartData['Online'], array(
                'type'   => 'line',
                'config' => $config
            ));
            $chart->addChart('LoginUser', $chartData['LoginUser'], array(
                'type'   => 'line',
                'config' => $config
            ));
            $chart->addChart('PayUser', $chartData['PayMoney'], array(
                'type'   => 'line',
                'config' => $config
            ));
            $chart->addChart('RoleUser', $chartData['RoleUser'] + $chartData['AccountUser'], array(
                'type'   => 'line',
                'config' => $config
            ));

            $chart->setShowType('float');
        }else{

            if($flag == true){

                $sids = array_keys($chartData['Online']);
                foreach($sids as $sid){
                    $con[$sid] = array('symbolSize' => 0);
                }
                $config['series'] = $con;
            }

            $chart->addChart('chart1', $chartData[$params['kpi']], array(
                'type'   => 'line',
                'config' => $config,
                'name'	 => $kpiOptions[$params['kpi']]
            ));

        }

        $this->out['filter'] = $filter->show();
        $this->out['grid']   = $grid->show();
        $this->out['chart']  = $chart->show();    }

    public function visit(){
        $this->setTitle('实时访问用户');

        $filter = new PubFilter();
        $grid   = new PubGrid();

        $filter = new PubFilter();
        $filter->addFilter('date', 'input', PubFunc::t('日期'), array('value' => date('Y-m-d'), 'time' => true));

        $header = RealTimeService::visitHeader();
        $data   = RealTimeService::visitData();

        $grid->addGrid($header, $data, array(
            'name' => PubFunc::t('数据')
        ));

        $this->out['filter'] = $filter->show();
        $this->out['grid']   = $grid->show();
    }
} 
